#include "MapToolPan.h"
#include "MapCanvas.h"
#include "Cursors.h"
#include <TGS/MapControl2.0/MapToPixel.h>
#include <TGS/GeoMath2.0/Point.h>
#include <TGS/GeoMath2.0/Envelope.h>
#include <QBitmap>
#include <QCursor>
#include <QMouseEvent>

namespace TGS{
namespace MapControl{


MapToolPan::MapToolPan( MapCanvas* canvas )
    : MapTool( canvas )
	, _dragging( false )
{
	setCursor(Qt::CrossCursor);
}


void MapToolPan::canvasMoveEvent( QMouseEvent * e )
{
  if (( e->buttons() & Qt::LeftButton ) )
  {
    _dragging = true;
    // move map and other canvas items
    canvas()->panAction( e );
  }
}

void MapToolPan::canvasReleaseEvent( QMouseEvent * e )
{
	if ( e->button() == Qt::LeftButton )
	{
		setCursor(Qt::CrossCursor);
		if ( _dragging )
		{
			canvas()->panActionEnd( e->pos() );
			_dragging = false;
		}
		else // add pan to mouse cursor
		{
			// transform the mouse pos to map coordinates
			/* QPoint centerTemp = canvas()->getCoordinateTransform()->mapToScreen( e->x(), e->y() );
			Point center(centerTemp.x(), centerTemp.y());
			canvas()->setExtent(TGS::GeoMath::Envelope( center, center ) );
			canvas()->refresh();*/
		}
	}
}

void MapToolPan::canvasPressEvent( QMouseEvent * e )
{
	if (( e->buttons() & Qt::LeftButton ) )
	{
		// set cursor
		QBitmap panBmp = QBitmap::fromData( QSize( 16, 16 ), pan_bits );
		QBitmap panBmpMask = QBitmap::fromData( QSize( 16, 16 ), pan_mask_bits );
		setCursor(QCursor( panBmp, panBmpMask, 5, 5 ));
	}
}



}//MapControl
}//TGS
